<?php


namespace App\Models\templates;


use App\Models\Model;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * App\Models\templates\Region
 *
 * @property int $id ID
 * @property int|null $pid 父id
 * @property string|null $shortname 简称
 * @property string|null $name 名称
 * @property string|null $merger_name 全称
 * @property int|null $level 层级 1 2 3 省市区县
 * @property string|null $pinyin 拼音
 * @property string|null $code 长途区号
 * @property string|null $zip_code 邮编
 * @property string|null $first 首字母
 * @property string|null $lng 经度
 * @property string|null $lat 纬度
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereFirst($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereLat($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereLng($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereMergerName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region wherePinyin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereShortname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\templates\Region whereZipCode($value)
 * @mixin \Eloquent
 */
class Region extends Model
{
    /**
     * 获取所有地区(树状结构)
     * @return mixed
     */
    public static function getCacheTree()
    {
        return self::regionCache()['tree'];
    }

    /**
     * 获取所有地区
     * @return mixed
     */
    public static function getCacheAll()
    {
        return self::regionCache()['all'];
    }

    /**
     * 获取地区缓存
     * @return mixed
     */
    private static function regionCache()
    {

        if (!Cache::get('region')) {
            foreach (self::all()->toArray() as $k => $value){
                $allData[$value['id']] = [
                    'id' => $value['id'],
                    'pid' => $value['pid'],
                    'name' => $value['name'],
                    'level' => $value['level'],
                ];
            }
            $all = $allData;
            // 格式化
            $tree = [];
            foreach ($allData as $pKey => $province) {
                if ($province['level'] == 1) {    // 省份
                    $tree[$province['id']] = $province;
                    unset($allData[$pKey]);
                    foreach ($allData as $cKey => $city) {
                        if ($city['level'] == 2 && $city['pid'] == $province['id']) {    // 城市
                            $tree[$province['id']]['city'][$city['id']] = $city;
                            unset($allData[$cKey]);
                            foreach ($allData as $rKey => $region) {
                                if ($region['level'] == 3 && $region['pid'] == $city['id']) {    // 地区
                                    $tree[$province['id']]['city'][$city['id']]['region'][$region['id']] = $region;
                                    unset($allData[$rKey]);
                                }
                            }
                        }
                    }
                }
            }
        }
        return ['all' => $all,'tree' => $tree];
    }
}